﻿using AutoMapper;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.Common;
using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace Core.SimpleTemp.Api.Api.YH
{
    /// <summary>
    /// JG_STOPObserveMain
    ///</summary>
    [ApiExplorerSettings(GroupName = "YH")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class JG_STOPObserveMainController : CoreApiController
    {
        private readonly JG_STOPObserveMainService _service;
        private readonly SysUserAppService _userService;
        private readonly string _CUSTOMTYPE1 = "Custom1";//自定义类型1和2，分别标识每个大项下的两个输入框
        private readonly string _CUSTOMTYPE2 = "Custom2";
        /// <summary>
        /// JG_STOPObserveMain
        ///</summary>
        public JG_STOPObserveMainController(JG_STOPObserveMainService service, SysUserAppService userService)
        {
            _service = service;
            _userService = userService;
        }

        /// <summary>
        /// 获取已部门为层级的用户数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetTreeDepartmentStaff")]
        public async Task<ActionResult> GetTreeDepartmentStaff()
        {
            var id = UserIdentity.Id;
            var dto = await _userService.GetAsync(id);
            var factory = dto.SysUserInfor.WorkCompany;

            var datatabe = _service.GetData(factory);
            return this.JsonSuccess(datatabe);
        }

        /// <summary>
        /// 获取统计页面列表数据
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetStatisticsList")]
        public async Task<IActionResult> GetStatisticsList(PagingModel<JG_STOPObserveMainDto>
                pagingQueryModel)
        {
            pagingQueryModel.Limit = 999;
            if (pagingQueryModel.Model.ObservationtimeStart != null)
            {
                pagingQueryModel.Model.ObservationtimeStart = ((DateTime)pagingQueryModel.Model.ObservationtimeStart).ToLocalTime();
            }
            if (pagingQueryModel.Model.ObservationtimeEnd != null)
            {
                pagingQueryModel.Model.ObservationtimeEnd = ((DateTime)pagingQueryModel.Model.ObservationtimeEnd).ToLocalTime();
            }
            //前台传的是v,在后台转换成c进行持久化
            if (pagingQueryModel.Model.evaluationv != null && pagingQueryModel.Model.evaluationv != "")
            {
                if (pagingQueryModel.Model.evaluationv == "0")
                {
                    pagingQueryModel.Model.evaluationc = false;
                }
                else
                {
                    pagingQueryModel.Model.evaluationc = true;
                }
            }
            else
            {
                pagingQueryModel.Model.evaluationc = null;
            }
            var result = await _service.GetList(pagingQueryModel);
            var smallTypes = _service.GetSmallTypeList();

            foreach (var i in result.PageData)
            {
                i.ryfy_remark = "";
                i.grfhzb_remark = "";
                i.rydwz_remark = "";
                i.gjjsb_remark = "";
                i.cx_remark = "";
                i.zxbz_remark = "";
                List<JG_STOPObserveDetailDto> details = _service.GetDetailList(i.Id).Where(x=>x.IsNotSafe==true).ToList();
                var detailGroup = details.GroupBy(x => x.BigType);
                foreach (var d in detailGroup)
                {
                    switch (d.Key)
                    {
                        case "BT01":
                            {
                                foreach (var st in d)
                                {
                                    if (st.remark == "Custom1" || st.remark == "Custom2")
                                    {
                                        i.ryfy_remark += st.SmallType+"<br />";
                                    }
                                    else
                                    {
                                        i.ryfy_remark += smallTypes.FirstOrDefault(x => x.SmallTypeCode == st.SmallType).SmallTypeName + "<br />";
                                    }
                                }
                                break;
                            }
                        case "BT02":
                            {
                                foreach (var st in d)
                                {
                                    if (st.remark == "Custom1" || st.remark == "Custom2")
                                    {
                                        i.grfhzb_remark += st.SmallType + "<br />";
                                    }
                                    else
                                    {
                                        i.grfhzb_remark += smallTypes.FirstOrDefault(x => x.SmallTypeCode == st.SmallType).SmallTypeName + "<br />";
                                    }
                                }
                                break;
                            }
                        case "BT03":
                            {
                                foreach (var st in d)
                                {
                                    if (st.remark == "Custom1" || st.remark == "Custom2")
                                    {
                                        i.rydwz_remark += st.SmallType + "<br />";
                                    }
                                    else
                                    {
                                        i.rydwz_remark += smallTypes.FirstOrDefault(x => x.SmallTypeCode == st.SmallType).SmallTypeName + "<br />";
                                    }
                                }
                                break;
                            }
                        case "BT04":
                            {
                                foreach (var st in d)
                                {
                                    if (st.remark == "Custom1" || st.remark == "Custom2")
                                    {
                                        i.gjjsb_remark += st.SmallType + "<br />";
                                    }
                                    else
                                    {
                                        i.gjjsb_remark += smallTypes.FirstOrDefault(x => x.SmallTypeCode == st.SmallType).SmallTypeName + "<br />";
                                    }
                                }
                                break;
                            }
                        case "BT05":
                            {
                                foreach (var st in d)
                                {
                                    if (st.remark == "Custom1" || st.remark == "Custom2")
                                    {
                                        i.cx_remark += st.SmallType + "<br />";
                                    }
                                    else
                                    {
                                        i.cx_remark += smallTypes.FirstOrDefault(x => x.SmallTypeCode == st.SmallType).SmallTypeName + "<br />";
                                    }
                                }
                                break;
                            }
                        case "BT06":
                            {
                                foreach (var st in d)
                                {
                                    if (st.remark == "Custom1" || st.remark == "Custom2")
                                    {
                                        i.zxbz_remark += st.SmallType + "<br />";
                                    }
                                    else
                                    {
                                        i.zxbz_remark += smallTypes.FirstOrDefault(x => x.SmallTypeCode == st.SmallType).SmallTypeName + "<br />";
                                    }
                                }
                                break;
                            }
                        default:break;
                    }
                }
            }

            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 获得统计数据合计项
        /// </summary>
       
        /// <returns></returns>
        [HttpPost("GetStatisticsSummary")]
        public async Task<JsonResult> GetStatisticsSummary(PagingModel<JG_STOPObserveMainDto>
                pagingQueryModel)
        {
            if (pagingQueryModel.Model.ObservationtimeStart != null)
            {
                pagingQueryModel.Model.ObservationtimeStart = ((DateTime)pagingQueryModel.Model.ObservationtimeStart).ToLocalTime();
            }
            if (pagingQueryModel.Model.ObservationtimeEnd != null)
            {
                pagingQueryModel.Model.ObservationtimeEnd = ((DateTime)pagingQueryModel.Model.ObservationtimeEnd).ToLocalTime();
            }
            var start = pagingQueryModel.Model.ObservationtimeStart??DateTime.MinValue;
            var end = pagingQueryModel.Model.ObservationtimeEnd??DateTime.MaxValue;

            pagingQueryModel.Offset = 0;
            pagingQueryModel.Limit = 999;

            IPageModel<JG_STOPObserveMainDto> data = await _service.GetList(pagingQueryModel);
            var sourceData = data.PageData;
            
            int ryfysafe = sourceData.Count(x => x.ryfy_IsSafety == true);
            int ryfyunsafe = sourceData.Count(x => x.ryfy_IsSafety == false);

            int grfhzbsafe = sourceData.Count(x => x.grfhzb_IsSafety == true);
            int grfhzbunsafe = sourceData.Count(x => x.grfhzb_IsSafety == false);

            int rydwzsafe = sourceData.Count(x => x.rydwz_IsSafety == true);
            int rydwzunsafe = sourceData.Count(x => x.rydwz_IsSafety == false);

            int gjjsbsafe = sourceData.Count(x => x.gjjsb_IsSafety == true);
            int gjjsbunsafe = sourceData.Count(x => x.gjjsb_IsSafety == false);

            int cxsafe = sourceData.Count(x => x.cx_IsSafety == true);
            int cxunsafe = sourceData.Count(x => x.cx_IsSafety == false);

            int zxbzsafe = sourceData.Count(x => x.zxbz_IsSafety == true);
            int zxbzunsafe = sourceData.Count(x => x.zxbz_IsSafety == false);


            var result = new
            {
                ryfy = "安全:"+ryfysafe+" 不安全:"+ryfyunsafe,
                grfhzb = "安全:" + grfhzbsafe + " 不安全:" + grfhzbunsafe,
                rydwz = "安全:" + rydwzsafe + " 不安全:" + rydwzunsafe,
                gjjsb = "安全:" + gjjsbsafe + " 不安全:" + gjjsbunsafe,
                cx = "安全:" + cxsafe + " 不安全:" + cxunsafe,
                zxbz = "安全:" + zxbzsafe + " 不安全:" + zxbzunsafe,
            };

            return this.JsonSuccess(result);
        }

        //public class TestModel
        //{ 
        //    public string Title { get; set; }
        //    public DateTime? Date { get; set; }
        //}
        //[HttpPost("Test")]
        //public void Test(TestModel model)
        //{
        //    string testStr = model.Title;
        //    model.Date = ((DateTime)model.Date).ToLocalTime();
        //}

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(JG_STOPObserveMainController) + "_" + nameof(JG_STOPObserveMainController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<JG_STOPObserveMainDto>
                pagingQueryModel)
        {
            
            //将日期转成本地时间
            if (pagingQueryModel.Model.ObservationtimeStart != null)
            {
                pagingQueryModel.Model.ObservationtimeStart = ((DateTime)pagingQueryModel.Model.ObservationtimeStart).ToLocalTime();
            }
            if (pagingQueryModel.Model.ObservationtimeEnd != null)
            {
                pagingQueryModel.Model.ObservationtimeEnd = ((DateTime)pagingQueryModel.Model.ObservationtimeEnd).ToLocalTime();
            }

            //前台传的是v,在后台转换成c进行持久化
            if (pagingQueryModel.Model.evaluationv != null && pagingQueryModel.Model.evaluationv != "")
            {
                if (pagingQueryModel.Model.evaluationv == "0")
                {
                    pagingQueryModel.Model.evaluationc = false;
                }
                else
                {
                    pagingQueryModel.Model.evaluationc = true;
                }
            }
            else
            {
                pagingQueryModel.Model.evaluationc = null;
            }

            IPageModel<JG_STOPObserveMainDto>
                result = await _service.GetList(pagingQueryModel);
            foreach (var i in result.PageData)
            {
                if (i.evaluationc.HasValue)
                {
                    i.evaluationText = (bool)i.evaluationc ? "是" : "否";
                }
            }
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.evaluationc,//所有皆安全
                a.evaluationText,
                a.department,//部门
                a.departmentText,//部门字面
                a.observePeople,//观察人ID
                a.observePeopleText,//观察人
                a.observaDate,//观察日期
                observaDateText = ((DateTime)a.observaDate).ToShortDateString(),
                a.observation,//观察时间
                a.shifts,//班次
                a.shiftsText,//班次字面
                a.situation,//区域
                a.situationText,//区域字面
                a.encourage,//安全行为
                a.correct,//不安全行为
                a.State,//状态
                a.CreateFactory,//隶属工厂
                a.CreateDept,//添加部门
                a.UpdateDept,//更改部门
                a.ryfy_remark,//人员反映备注
                a.grfhzb_remark,//个人防护装备备注
                a.rydwz_remark,//人员的位子备注
                a.gjjsb_remark,//工具及设备备注
                a.cx_remark,//程序备注
                a.zxbz_remark,//秩序标准秩序标准
                a.ryfy_IsSafety,//人员反映其他是否安全
                a.grfhzb_IsSafety,//个人防护装备是否安全
                a.rydwz_IsSafety,//人员的位置是否安全
                a.gjjsb_IsSafety,//工具及设备是否安全
                a.cx_IsSafety,//程序是否安全
                a.zxbz_IsSafety,//秩序标准
            }
            ).ToList();
            
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(JG_STOPObserveMainController) + "_" + nameof(JG_STOPObserveMainController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(JG_STOPObserveMainDto dto)
        {
            dto.observePeople = UserIdentity.Id;
            dto.department = await this.GetUserDepartment(UserIdentity.Id);

            //检查是否存在大小类逻辑
            //不能出现大类不勾选，小类也不勾选的情况
            bool isPass = true;
            string errorMsg = "未做选择：";
            if (dto.ryfy_IsSafety == null || ((bool)dto.ryfy_IsSafety) == false)
            {
                var subCheck = !dto.ryfyCheckList.Exists(x => x.IsNotSafe == true);
                var subInput1 = !dto.ryfyInput1.IsNotSafe;
                var subInput2 = !dto.ryfyInput2.IsNotSafe;

                var sub = subCheck && subInput1 && subInput2;
                if (sub)
                { isPass = false;errorMsg += "人员的反应 "; }
            }
            if (dto.grfhzb_IsSafety == null || ((bool)dto.grfhzb_IsSafety) == false)
            {
                var subCheck = !dto.grfhzbCheckList.Exists(x => x.IsNotSafe == true);
                var subInput1 = !dto.grfhzbInput1.IsNotSafe;
                var subInput2 = !dto.grfhzbInput2.IsNotSafe;

                var sub = subCheck && subInput1 && subInput2;
                if (sub)
                { isPass = false; errorMsg += "个人防护装备 "; }
            }
            if (dto.rydwz_IsSafety == null || ((bool)dto.rydwz_IsSafety) == false)
            {
                var subCheck = !dto.rydwzCheckList.Exists(x => x.IsNotSafe == true);
                var subInput1 = !dto.rydwzInput1.IsNotSafe;
                var subInput2 = !dto.rydwzInput2.IsNotSafe;

                var sub = subCheck && subInput1 && subInput2;
                if (sub)
                { isPass = false; errorMsg += "人员的位置 "; }
            }
            if (dto.gjjsb_IsSafety == null || ((bool)dto.gjjsb_IsSafety) == false)
            {
                var subCheck = !dto.gjjsbCheckList.Exists(x => x.IsNotSafe == true);
                var subInput1 = !dto.gjjsbInput1.IsNotSafe;

                var sub = subCheck && subInput1;
                if (sub)
                { isPass = false; errorMsg += "工具及设备 "; }
            }
            if (dto.cx_IsSafety == null || ((bool)dto.cx_IsSafety) == false)
            {
                var subCheck = !dto.cxCheckList.Exists(x => x.IsNotSafe == true);
                var subInput1 = !dto.cxInput1.IsNotSafe;

                var sub = subCheck && subInput1;
                if (sub)
                { isPass = false; errorMsg += "程序 "; }
            }
            if (dto.zxbz_IsSafety == null || ((bool)dto.zxbz_IsSafety) == false)
            {
                var subCheck = !dto.zxbzCheckList.Exists(x => x.IsNotSafe == true);
                var subInput1 = !dto.zxbzInput1.IsNotSafe;

                var sub = subCheck && subInput1;
                if (sub)
                { isPass = false; errorMsg += "秩序标准 "; }
            }
            if (!isPass)
            {
                return this.JsonFaild(errorMsg);
            }
            
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                dto.observaDate = ((DateTime)dto.observaDate).ToLocalTime().Date;
                dto.evaluationc = (bool)dto.ryfy_IsSafety && (bool)dto.grfhzb_IsSafety && (bool)dto.rydwz_IsSafety && (bool)dto.gjjsb_IsSafety && (bool)dto.cx_IsSafety && (bool)dto.zxbz_IsSafety;
                var main = await _service.InsertAsync(dto);
                //人员的反应复选框
                foreach (var i in dto.ryfyCheckList)
                {
                    i.STOPObserveMain = main.Id;
                    await _service.InsertDetailAsync(i);
                }
     
                //人员的反应输入框1
                dto.ryfyInput1.STOPObserveMain = main.Id;
                dto.ryfyInput1.remark = _CUSTOMTYPE1;
                dto.ryfyInput1.SmallType = dto.ryfyInput1.SmallTypeText;
                await _service.InsertDetailAsync(dto.ryfyInput1);
                //人员的反应输入框2
                dto.ryfyInput2.STOPObserveMain = main.Id;
                dto.ryfyInput2.remark = _CUSTOMTYPE2;
                dto.ryfyInput2.SmallType = dto.ryfyInput2.SmallTypeText;
                await _service.InsertDetailAsync(dto.ryfyInput2);

                //个人防护装备
                foreach (var i in dto.grfhzbCheckList)
                {
                    i.STOPObserveMain = main.Id;
                    await _service.InsertDetailAsync(i);
                }

                //个人防护装备输入框1
                dto.grfhzbInput1.STOPObserveMain = main.Id;
                dto.grfhzbInput1.remark = _CUSTOMTYPE1;
                dto.grfhzbInput1.SmallType = dto.grfhzbInput1.SmallTypeText;
                await _service.InsertDetailAsync(dto.grfhzbInput1);
                //个人防护装备输入框2
                dto.grfhzbInput2.STOPObserveMain = main.Id;
                dto.grfhzbInput2.remark = _CUSTOMTYPE2;
                dto.grfhzbInput2.SmallType = dto.grfhzbInput2.SmallTypeText;
                await _service.InsertDetailAsync(dto.grfhzbInput2);

                //人员的位置
                foreach (var i in dto.rydwzCheckList)
                {
                    i.STOPObserveMain = main.Id;
                    await _service.InsertDetailAsync(i);
                }

                //人员的位置输入框1
                dto.rydwzInput1.STOPObserveMain = main.Id;
                dto.rydwzInput1.remark = _CUSTOMTYPE1;
                dto.rydwzInput1.SmallType = dto.rydwzInput1.SmallTypeText;
                await _service.InsertDetailAsync(dto.rydwzInput1);
                //人员的位置输入框2
                dto.rydwzInput2.STOPObserveMain = main.Id;
                dto.rydwzInput2.remark = _CUSTOMTYPE2;
                dto.rydwzInput2.SmallType = dto.rydwzInput2.SmallTypeText;
                await _service.InsertDetailAsync(dto.rydwzInput2);


                //工具及设备
                foreach (var i in dto.gjjsbCheckList)
                {
                    i.STOPObserveMain = main.Id;
                    await _service.InsertDetailAsync(i);
                }

                //工具及设备输入框1
                dto.gjjsbInput1.STOPObserveMain = main.Id;
                dto.gjjsbInput1.remark = _CUSTOMTYPE1;
                dto.gjjsbInput1.SmallType = dto.gjjsbInput1.SmallTypeText;
                await _service.InsertDetailAsync(dto.gjjsbInput1);

                //程序
                foreach (var i in dto.cxCheckList)
                {
                    i.STOPObserveMain = main.Id;
                    await _service.InsertDetailAsync(i);
                }

                //程序输入框1
                dto.cxInput1.STOPObserveMain = main.Id;
                dto.cxInput1.remark = _CUSTOMTYPE1;
                dto.cxInput1.SmallType = dto.cxInput1.SmallTypeText;
                await _service.InsertDetailAsync(dto.cxInput1);


                //秩序标准
                foreach (var i in dto.zxbzCheckList)
                {
                    i.STOPObserveMain = main.Id;
                    await _service.InsertDetailAsync(i);
                }

                //秩序标准输入框1
                dto.zxbzInput1.STOPObserveMain = main.Id;
                dto.zxbzInput1.remark = _CUSTOMTYPE1;
                dto.zxbzInput1.SmallType = dto.zxbzInput1.SmallTypeText;
                await _service.InsertDetailAsync(dto.zxbzInput1);



                return this.JsonSuccess(string.Empty);
            }
            else
            {
                dto.evaluationc = (bool)dto.ryfy_IsSafety && (bool)dto.grfhzb_IsSafety && (bool)dto.rydwz_IsSafety && (bool)dto.gjjsb_IsSafety && (bool)dto.cx_IsSafety && (bool)dto.zxbz_IsSafety;
                dto.observaDate = ((DateTime)dto.observaDate).ToLocalTime().Date;
                await _service.UpdateAsync(dto);
                //人员反应复选框
                foreach (var i in dto.ryfyCheckList)
                {
                    await _service.UpdateDetailAsync(i);
                }
                //人员反应输入框1
                dto.ryfyInput1.SmallType = dto.ryfyInput1.SmallTypeText;
                dto.ryfyInput2.SmallType = dto.ryfyInput2.SmallTypeText;
                await _service.UpdateDetailAsync(dto.ryfyInput1);
                await _service.UpdateDetailAsync(dto.ryfyInput2);

                //个人防护装备
                foreach (var i in dto.grfhzbCheckList)
                {
                    await _service.UpdateDetailAsync(i);
                }
                //个人防护装备输入框
                dto.grfhzbInput1.SmallType = dto.grfhzbInput1.SmallTypeText;
                dto.grfhzbInput2.SmallType = dto.grfhzbInput2.SmallTypeText;
                await _service.UpdateDetailAsync(dto.grfhzbInput1);
                await _service.UpdateDetailAsync(dto.grfhzbInput2);

                //人员的位置
                foreach (var i in dto.rydwzCheckList)
                {
                    await _service.UpdateDetailAsync(i);
                }
                //个人防护装备输入框
                dto.rydwzInput1.SmallType = dto.rydwzInput1.SmallTypeText;
                dto.rydwzInput2.SmallType = dto.rydwzInput2.SmallTypeText;
                await _service.UpdateDetailAsync(dto.rydwzInput1);
                await _service.UpdateDetailAsync(dto.rydwzInput2);

                //工具及设备
                foreach (var i in dto.gjjsbCheckList)
                {
                    await _service.UpdateDetailAsync(i);
                }
                //工具及设备
                dto.gjjsbInput1.SmallType = dto.gjjsbInput1.SmallTypeText;
                await _service.UpdateDetailAsync(dto.gjjsbInput1);

                //程序
                foreach (var i in dto.cxCheckList)
                {
                    await _service.UpdateDetailAsync(i);
                }
                //程序输入框
                dto.cxInput1.SmallType = dto.cxInput1.SmallTypeText;
                await _service.UpdateDetailAsync(dto.cxInput1);

                //程序
                foreach (var i in dto.zxbzCheckList)
                {
                    await _service.UpdateDetailAsync(i);
                }
                //程序输入框
                dto.zxbzInput1.SmallType = dto.zxbzInput1.SmallTypeText;
                await _service.UpdateDetailAsync(dto.zxbzInput1);


                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(JG_STOPObserveMainController) + "_" + nameof(JG_STOPObserveMainController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {
            foreach (var main in ids)
            {
                var details = _service.GetDetailList(main);
                await _service.DeleteDetailBatchAsync(details.Select(x => x.Id).ToArray());
            }
            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }

      

        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var main = await _service.GetAsync(id);
            var ret = Mapper.Map<JG_STOPObserveMainDto>(main);

            var details = _service.GetDetailList(id);
            //人员的反应复选框
            ret.ryfyCheckList = details.Where(x => x.BigType == "BT01" && x.remark != _CUSTOMTYPE1 && x.remark != _CUSTOMTYPE2).OrderBy(x => x.UpdateTime).ToList();
            //人员的反应输入框1
            ret.ryfyInput1 = details.FirstOrDefault(x => x.BigType == "BT01" && x.remark == _CUSTOMTYPE1);
            ret.ryfyInput1.SmallTypeText = ret.ryfyInput1.SmallType;
            //人员的反应输入框2
            ret.ryfyInput2 = details.FirstOrDefault(x => x.BigType == "BT01" && x.remark == _CUSTOMTYPE2);
            ret.ryfyInput2.SmallTypeText = ret.ryfyInput2.SmallType;

            //个人防护装备复选框
            ret.grfhzbCheckList = details.Where(x => x.BigType == "BT02" && x.remark != _CUSTOMTYPE1 && x.remark != _CUSTOMTYPE2).OrderBy(x => x.UpdateTime).ToList();
            //输入框1
            ret.grfhzbInput1 = details.FirstOrDefault(x => x.BigType == "BT02" && x.remark == _CUSTOMTYPE1);
            ret.grfhzbInput1.SmallTypeText = ret.grfhzbInput1.SmallType;
            //输入框2
            ret.grfhzbInput2 = details.FirstOrDefault(x => x.BigType == "BT02" && x.remark == _CUSTOMTYPE2);
            ret.grfhzbInput2.SmallTypeText = ret.grfhzbInput2.SmallType;

            //人员的位置复选框
            ret.rydwzCheckList = details.Where(x => x.BigType == "BT03" && x.remark != _CUSTOMTYPE1 && x.remark != _CUSTOMTYPE2).OrderBy(x => x.UpdateTime).ToList();
            //输入框1
            ret.rydwzInput1 = details.FirstOrDefault(x => x.BigType == "BT03" && x.remark == _CUSTOMTYPE1);
            ret.rydwzInput1.SmallTypeText = ret.rydwzInput1.SmallType;
            //输入框2
            ret.rydwzInput2 = details.FirstOrDefault(x => x.BigType == "BT03" && x.remark == _CUSTOMTYPE2);
            ret.rydwzInput2.SmallTypeText = ret.rydwzInput2.SmallType;


            //工具及设备复选框
            ret.gjjsbCheckList = details.Where(x => x.BigType == "BT04" && x.remark != _CUSTOMTYPE1 && x.remark != _CUSTOMTYPE2).OrderBy(x => x.UpdateTime).ToList();
            //输入框1
            ret.gjjsbInput1 = details.FirstOrDefault(x => x.BigType == "BT04" && x.remark == _CUSTOMTYPE1);
            ret.gjjsbInput1.SmallTypeText = ret.gjjsbInput1.SmallType;

            //程序复选框
            ret.cxCheckList = details.Where(x => x.BigType == "BT05" && x.remark != _CUSTOMTYPE1 && x.remark != _CUSTOMTYPE2).OrderBy(x => x.UpdateTime).ToList();
            //输入框1
            ret.cxInput1 = details.FirstOrDefault(x => x.BigType == "BT05" && x.remark == _CUSTOMTYPE1);
            ret.cxInput1.SmallTypeText = ret.cxInput1.SmallType;

            //秩序标准复选框
            ret.zxbzCheckList = details.Where(x => x.BigType == "BT06" && x.remark != _CUSTOMTYPE1 && x.remark != _CUSTOMTYPE2).OrderBy(x => x.UpdateTime).ToList();
            //输入框1
            ret.zxbzInput1 = details.FirstOrDefault(x => x.BigType == "BT06" && x.remark == _CUSTOMTYPE1);
            ret.zxbzInput1.SmallTypeText = ret.zxbzInput1.SmallType;


            return this.JsonSuccess(ret);



            #region 废弃
            //var model = await _service.GetAsync(id);
            //if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            //var retObj = new
            //{
            //    model.evaluationc,//所有皆安全
            //    model.observePeople,//观察人
            //    model.department,//部门
            //    model.shifts,//班次
            //    model.situation,//区域
            //    model.observaDate,//观察日期

            //    //model.observation,//观察时间
            //    model.encourage,//安全行为
            //    model.correct,//不安全行为
            //    //model.State,//状态
            //    //model.CreateFactory,//隶属工厂
            //    //model.CreateDept,//添加部门
            //    //model.UpdateDept,//更改部门

            //    model.ryfy_remark,//人员反映备注
            //    model.grfhzb_remark,//个人防护装备备注
            //    model.rydwz_remark,//人员的位子备注
            //    model.gjjsb_remark,//工具及设备备注
            //    model.cx_remark,//程序备注
            //    model.zxbz_remark,//秩序标准秩序标准

            //    model.ryfy_IsSafety,//人员反映其他是否安全
            //    model.grfhzb_IsSafety,//个人防护装备是否安全
            //    model.rydwz_IsSafely,//人员的位置是否安全
            //    model.jjsb_IsSafely,//工具及设备是否安全
            //    model.cx_IsSafely,//程序是否安全
            //    model.zxbz_IsSafely,//秩序标准
            //    JG_STOPObserveDetail = model.JG_STOPObserveDetail.Select(a => new { a.BigType, a.SmallType })
            //};
            //return this.JsonSuccess(retObj);
            #endregion
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(JG_STOPObserveMainController) + "_" + nameof(JG_STOPObserveMainController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<JG_STOPObserveMainDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }
        #endregion

        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("ExportStatistic")]
        [PermissionFilter(nameof(JG_STOPObserveMainController) + "_" + nameof(JG_STOPObserveMainController.ExportStatistic), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            ExportStatistic(PagingModel<JG_STOPObserveMainDto>
                pagingQueryModel)
        {
            var fileName = await _service.ExportStatistic(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }


        /// <summary>
        /// 获得大类型数据字典用于填充前台UI的文字
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetBigTypeList")]
        public JsonResult GetBigTypeList()
        {
            var bigTypeList = _service.GetBigTypeList();
            var ret = new
            {
                ryfy = bigTypeList.FirstOrDefault(x => x.BigTypeCode == "BT01").BigTypeName,
                grfhzb = bigTypeList.FirstOrDefault(x => x.BigTypeCode == "BT02").BigTypeName,
                rydwz = bigTypeList.FirstOrDefault(x => x.BigTypeCode == "BT03").BigTypeName,
                gjjsb = bigTypeList.FirstOrDefault(x => x.BigTypeCode == "BT04").BigTypeName,
                cx = bigTypeList.FirstOrDefault(x => x.BigTypeCode == "BT05").BigTypeName,
                zxbz = bigTypeList.FirstOrDefault(x => x.BigTypeCode == "BT06").BigTypeName
            };
            return this.JsonSuccess(ret);
        }

        /// <summary>
        /// 获得小类型数据字典用于填充小项UI文字
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetSmallTypeList")]
        public JsonResult GetSmallTypeList()
        {
            List<JG_STOPObserveDetailDto> ryfyDetailList = new List<JG_STOPObserveDetailDto>();
            List<JG_STOPObserveDetailDto> grfhzbDetailList = new List<JG_STOPObserveDetailDto>();
            List<JG_STOPObserveDetailDto> rydwzDetailList = new List<JG_STOPObserveDetailDto>();
            List<JG_STOPObserveDetailDto> gjjsbDetailList = new List<JG_STOPObserveDetailDto>();
            List<JG_STOPObserveDetailDto> cxDetailList = new List<JG_STOPObserveDetailDto>();
            List<JG_STOPObserveDetailDto> zxbzDetailList = new List<JG_STOPObserveDetailDto>();
            var smallTypeList = _service.GetSmallTypeList();
            if (smallTypeList != null && smallTypeList.Count > 0)
            {
                foreach (var group in smallTypeList.GroupBy(x => x.BigTypeCode))
                {
                    foreach (var item in group)
                    {
                        var dto = new JG_STOPObserveDetailDto();
                        dto.BigType = item.BigTypeCode;
                        dto.IsDeleted = false;
                        dto.SmallType = item.SmallTypeCode;
                        dto.SmallTypeText = item.SmallTypeName;
                        dto.IsNotSafe = false;
                        switch (item.BigTypeCode)
                        {
                            case "BT01": ryfyDetailList.Add(dto); break;
                            case "BT02": grfhzbDetailList.Add(dto); break;
                            case "BT03": rydwzDetailList.Add(dto);break;
                            case "BT04": gjjsbDetailList.Add(dto); break;
                            case "BT05": cxDetailList.Add(dto); break;
                            case "BT06": zxbzDetailList.Add(dto); break;
                            default: break;
                        }
                    }
                }
                var ret = new
                {
                    ryfyDetailList = ryfyDetailList,
                    grfhzbDetailList = grfhzbDetailList,
                    rydwzDetailList = rydwzDetailList,
                    gjjsbDetailList = gjjsbDetailList,
                    cxDetailList = cxDetailList,
                    zxbzDetailList = zxbzDetailList,

                    //人员的反应input框的数据
                    ryfyInput1 = new JG_STOPObserveDetailDto {
                        BigType = "BT01",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    ryfyInput2 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT01",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    //个人防护装备input框的数据
                    grfhzbInput1 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT02",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    grfhzbInput2 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT02",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    //人员的位置input框的数据
                    rydwzInput1 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT03",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    rydwzInput2 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT03",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    //工具及设备input框的数据
                    gjjsbInput1 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT04",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    //程序input框的数据
                    cxInput1 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT05",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                    //秩序标准input框的数据
                    zxbzInput1 = new JG_STOPObserveDetailDto
                    {
                        BigType = "BT06",
                        IsDeleted = false,
                        SmallType = "",
                        SmallTypeText = "",
                        IsNotSafe = false
                    },
                };
                return this.JsonSuccess(ret);
            }
            return this.JsonSuccess();
        }

        /// <summary>
        /// 获得当前登录用户所在工厂的Guid
        /// </summary>

        /// <returns></returns>
        [HttpGet("GetCurrentUserFactory")]
        public async Task<IActionResult> GetCurrentUserFactory()
        {
            var id = UserIdentity.Id;
            var dto = await _userService.GetAsync(id);
            var rtn = dto.SysUserInfor.WorkCompany;
            return this.JsonSuccess(rtn);

        }

        /// <summary>
        /// 根据用户id获取其所在的部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetUserDepartment")]
        public async Task<Guid> GetUserDepartment(Guid id)
        {
            var deps = await _userService.UserDepartmentAsync(id);
            return deps.FirstOrDefault().Id;
        }


    }
}
